# Apply to: exit_3_cands.csv
# Description: W-NOMINATE of San Francisco Supervisoral Candidates

# Remove all objects just to be safe

rm(list=ls(all=TRUE))


# Load appropriate packages

# Standard

library(MASS)
library(stats)


# W-NOMINATE

library(pscl)
library(wnominate)
library(gdata)
library(ellipse)

# Read in data

sfsup1 <- read.table(".../1805 Roadmaps/Figures A7 and A8/exit_3_cands.csv", header=TRUE, sep=",")

dim(sfsup1)

head(sfsup1[,1:15])


## Format data

sfsup <- as.vector(sfsup1)
sfsup.2 <- sfsup[,-c(1,2,3)]
sfsup.names <- sfsup[,2]
legData <- matrix(sfsup[,3], length(sfsup[,3]),1)


## Create rollcall object

rc <- rollcall(data=sfsup.2, yea=c(1), nay=c(6), missing=c(9), notInLegis=0,
               legis.names=sfsup.names,
               legis.data=legData,
               desc="SF Supervisorial Candidates 2012", source="sfpublicpress.org")


# Run WNOMINATE
# Candidate Mike Garcia used to define polarity

set.seed(323598)

result <- wnominate(rcObject=rc, polarity=c(14,14))

summary(result)


# Assume 1-D model

set.seed(5387)

result1 <- wnominate(rcObject=rc, polarity=c(14), dim=1)

scores <- result1$legislators
write.csv(scores, ".../1805 Roadmaps/Figures A7 and A8/sfsup_1_scores.csv")


# Clean up

# rm(list=ls(all=TRUE))

# End
